<?php # 5.2.4

abstract
class CMArmy
{
	protected static $army = 0;
	
	private
		$soldiers = array()
	;
	
	public
	function CMArmy()
	{
		self::$army++;
	}
	
	final public function __get($name)
	{
		return $this->$name;
	}
	
	final public function __set($name, $value)
	{
		return $this->$name = $value;
	}
	
	final public function __call($fnc, $args){}
	
	final public
	function addSoldier(CMSoldier $soldier)
	{
		$this->soldiers[] = $soldier;
	}
	
	final public 
	function attack(CMArmy $defend)
	{
		# Atacan 3 al azar
		shuffle($this->soldiers);
		for($i=0; $i< min(count($this->soldiers), 3); $i++)
		{
			if($defend->defend($this->soldiers[$i]) === false)
				return false;
				
			sleep( 1 );
		}
		
		# Descansa 1 al azar
		$i = rand(0, count($this->soldiers)-1);
		$this->soldiers[$i]->rest();
	}
	
	final public 
	function defend(CMSoldier $attacker)
	{
		$i = rand(0, count($this->soldiers)-1);
		
		$attacker->attack( $this->soldiers[$i] );
		
		if(!$this->soldiers[$i]->isLive())
		{
			$attacker->upgrade();
			
			echo("{$this->name}: {$this->soldiers[$i]->id} Murio en combate\n");
			array_splice($this->soldiers, $i, 1);
			
			if(count($this->soldiers) == 0)
				return false;
		}
	}
	
	final public 
	function isDefeated()
	{
		return count($this->soldiers) == 0;
	}
	
	final public
	function getStat()
	{
		$stat = 0;
		foreach($this->soldiers as $i => $soldier)
			$stat += $soldier->life;
		
		return $stat;
	}
}
?>